$(function() {

    // console.log(11);
    $.ajax({
        type: 'get',
        url: bigNews.user_detail,
        headers: { 'Authorization': localStorage.getItem('bigNews_token') },
        dataType: 'json',
        success: function(res) {
            // console.log(res);
            $('#inputEmail1').val(res.data.username)
            $('#inputEmail2').val(res.data.nickname)
            $('#inputEmail3').val(res.data.email)
            $('.user_pic').attr('src', res.data.userPic)
            $('#inputEmail4').val(res.data.password)
        }
    })



    let formdata = new FormData()
    $('#exampleInputFile').on('change', function() {
        let myfile = $('#exampleInputFile')[0].files[0]
        if (myfile.type.indexOf('image/') != -1) {
            let url = URL.createObjectURL(myfile)
            $('.user_pic').attr('src', url)
            formdata.append('userPic', myfile)
        } else {
            alert('请选择图片格式文件')
            return
        }
    })

    $('.btn-edit').on('click', function() {

        let username = $('#inputEmail1').val()
        let nickname = $('#inputEmail2').val()
        let email = $('#inputEmail3').val()
        let password = $('#inputEmail4').val()

        formdata.append('username', username)
        formdata.append('nickname', nickname)
        formdata.append('email', email)
        formdata.append('password', password)
        $.ajax({
            type: 'post',
            url: bigNews.user_edit,
            headers: { 'Authorization': localStorage.getItem('bigNews_token') },
            data: formdata,
            dataType: 'json',
            processData: false,
            contentType: false,
            success: function(res) {
                alert(res.msg)
                top.location.href = './index.html'
            }
        })

    })

})